package com.baomidou.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.springboot.util.AjaxResult;
import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import com.baomidou.springboot.service.UserService;
import com.baomidou.springboot.entity.User;


/**
 * @描述：$!{table.comment}控制类
 * @作者: jack
 * @日期: 2021-12-06
 */
@RestController
@RequestMapping("/User")
@Api(tags = "API - UserController")
public class UserController{

    @Autowired
    public UserService userService;

    @Autowired
    private UserMapper userMapper;
    /**
     * @描述: 根据实体属性分页查询
     * @入参:
     * @出参:
     * @作者: jack
     * @日期: 2021-12-06
     **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    public AjaxResult listByEntity(Page page, User user){
        LambdaQueryWrapper<User> wrapper=new QueryWrapper().lambda();
        IPage<User> iPage= userService.page(page,wrapper);
        return AjaxResult.success(iPage);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: jack
     * @日期: 2021-12-06
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id){
        if(StrUtil.isBlank(id)){
            return new AjaxResult(AjaxResult.Type.ERROR,"id不能为空");
        }
        User user = userService.getById(id);
        return AjaxResult.success(user);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: jack
     * @日期: 2021-12-06
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public AjaxResult save(User user){
        prepareData(user,true);
        userService.save(user);
        return AjaxResult.success();
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: jack
     * @日期: 2021-12-06
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(User user){
        prepareData(user,false);
        userService.updateById(user);
        return AjaxResult.success();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: jack
     * @日期: 2021-12-06
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        userService.removeById(id);
        return AjaxResult.success();
    }


    private void prepareData(User user,boolean isSave){
    }

}